<template>
    <div class="container">
            <div class="login-wrapper">
                <div class="header">Register</div>
                <div class="form-wrapper">
                    <input type="text" name="username" placeholder="账户" class="input-item" v-model="user.username">
                    <input type="password" name="password" placeholder="密码" class="input-item" v-model="user.password">
                    <input type="password" name="repassword" placeholder="再次确认密码" class="input-item" v-model="repassword">
                    <div class="btn" @click="registerSuccess()">Register</div>
                </div>
            </div>
        </div>
</template>
    
<script lang="ts">
    export default {
        name:"Register"
    }
</script>

<script lang="ts" setup>
import { useRouter } from 'vue-router';
import { ref } from 'vue';

// 定义注册信息接口
export interface IUser {
    id: number;
    username: string;
    password: string;
}

// 注册信息表
const users = JSON.parse(localStorage.getItem('users') as string) as IUser[] || [];

// 注册信息
const user = ref<IUser>({
    id: users.length > 0? users[users.length - 1].id + 1 : 1,
    username: '',
    password: '',
});
const repassword = ref('');


// 判断用户名是否已存在
const isUsernameExist = (username: string) => {
    return users.some(item => item.username === username);
}
// 把注册信息提交到本地存储
const register = () => {
    if(user.value.password !== repassword.value) {
        alert('两次输入的密码不一致，请重新输入');
        return false;
    }
    if(isUsernameExist(user.value.username)) {
        alert('用户名已存在，请重新输入');
        return false;
    }
    users.push(user.value);
    localStorage.setItem('users', JSON.stringify(users));
    alert('注册成功，请登录');   // 注册成功后弹出提示信息
    return true;
}



const router = useRouter();       // 引入路由
const registerSuccess = () => {
    // 判断是否注册成功
    const isSuccess = register();
    // 注册成功后跳转到登录页面
    if(isSuccess) {
        router.back()
    } else if(isUsernameExist(user.value.username)) {
        user.value.username = '';
    }
    else {
        repassword.value = '';
    }
    
}
</script>

<style scoped>
html {
    height: 100%;
}
body {
    height: 100%;
}
.container {
    /* margin-top: 5%; */
    height: 980px;
    width: 100%;
    background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}
.login-wrapper {
    background-color: #fff;
    width: 358px;
    height: 588px;
    border-radius: 15px;
    padding: 0 50px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
.header {
    font-size: 38px;
    font-weight: bold;
    text-align: center;
    line-height: 200px;
}
.input-item {
    display: block;
    width: 100%;
    margin-bottom: 20px;
    border: 0;
    padding: 10px;
    border-bottom: 1px solid rgb(128, 125, 125);
    font-size: 15px;
    outline: none;
}
.input-item:placeholder {
    text-transform: uppercase;
}
.btn {
    text-align: center;
    padding: 10px;
    width: 100%;
    margin-top: 40px;
    background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
    color: #fff;
    margin: 0 auto;
}
.msg {
    text-align: center;
    line-height: 88px;
}
a {
    text-decoration-line: none;
    color: #abc1ee;
}

</style>